package mysql

import (
	"gorm.io/gorm"
	"onerk4/zg5yk/ordering/common/global"
	"onerk4/zg5yk/ordering/common/model"
)

func QueryTakeoutList(query *gorm.DB) (result []*model.Takeout, err error) {
	err = query.Find(&result).Error
	return
}

func UserRecommend(id int64) (result []*model.Takeout, err error) {
	err = global.DB.
		Model(&model.Takeout{}).
		Joins("LEFT JOIN orders ON takeouts.id = orders.takeout_id AND orders.uid", id).
		Select("takeouts.*, COUNT(orders.id) AS order_count").
		Group("takeouts.id").
		Order("order_count DESC").
		Find(&result).Error
	return
}

func UpdateTakeoutStock(id int, num int, tx *gorm.DB) error {
	return tx.Model(&model.Takeout{}).Where("id = ?", id).Limit(1).Update("meal_products_stock", num).Error
}
